package unit15.match;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class HrefMatch {
	public static void main(String[] args) {
		try {
//			get URL string from command line or use default
			String 	urlString;
			if(args.length>0){
				urlString=args[0];
			}
			else
				urlString="http://java.sun.com";
			
//			open reader for URL
			InputStreamReader in=new InputStreamReader(new URL(urlString).openStream());
//			read contents into string builder
			StringBuilder sb=new  StringBuilder();
			int ch;
			while((ch=in.read())!=-1){
				sb.append((char)ch);
			}
			
			String patternString="<a \\s+href\\s*=\\s*(\"[^\"]*\" | [^\\s>]*)\\s*>";
			Pattern pattern=Pattern.compile(patternString,Pattern.CASE_INSENSITIVE);
			Matcher matcher=pattern.matcher(sb);
			
			while(matcher.find()){
				int start=matcher.start();
				int end=matcher.end();
				String match=sb.substring(start,end);
				System.out.println(match);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch(PatternSyntaxException e){
			e.printStackTrace();
		}
	}
}
